package com.supercube.sport.yoga.controller;

import com.supercube.common.utils.TimeUtils;
import com.supercube.sport.yoga.bo.MemberChangeBo;
import com.supercube.sport.yoga.dao.HomeMapper;
import com.supercube.sport.yoga.service.HomeService;
import com.supercube.sport.yoga.utils.ResultInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Description：
 * Created By guowen
 */
@RestController
@RequestMapping("/member")
@Api(tags = "首页统计信息")
public class HomeController {
    @Autowired
    private HomeService homeService;
    @Autowired
    private HomeMapper homeMapper;

    @ApiOperation(value = "7/14/30天未来上课", notes = "7/14/30天未来上课")
    @PostMapping("/getFutureClassesCount")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "days", value = "7、14、30", dataType = "int", paramType = "query"),
    })
    public ResultInfo getFutureClassesCount(@RequestBody Map<String, Integer> map) {
        int count = homeService.getFutureClassesCount(map.get("days"));
        return ResultInfo.success(count);
    }

    @ApiOperation(value = "最近7/14/30天会员到期", notes = "最近7/14/30天会员到期")
    @PostMapping("/getMemberExpirationCount")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "days", value = "7、14、30", dataType = "int", paramType = "query"),
    })
    public ResultInfo getMemberExpirationCount(@RequestBody Map<String, Integer> map) {
        int count = homeService.getMemberExpirationCount(map.get("days"));
        return ResultInfo.success(count);
    }

    @ApiOperation(value = "最近7/14/30天会员生日", notes = "最近7/14/30天会员生日")
    @PostMapping("/getMemberBirthdayCount")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "days", value = "7、14、30", dataType = "int", paramType = "query"),
    })
    public ResultInfo getMemberBirthdayCount(@RequestBody Map<String, Integer> map) {
        int count = homeService.getMemberBirthdayCount(map.get("days"));
        return ResultInfo.success(count);
    }

    @ApiOperation(value = "本天/月/年新增会员数、失效会员数", notes = "本天/月/年新增会员数、失效会员数")
    @PostMapping("/getMemberChangeCount")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "day、month、year", dataType = "String", paramType = "query"),
    })
    public ResultInfo getMemberChangeCount(@RequestBody Map<String, String> map) {
        String type = map.get("type");
        // 2020-07-08 15:12:30
        String currentTime = TimeUtils.getCurrentTime();
        String sDate = currentTime;
        if (Objects.equals(type, "day")) {
            sDate = currentTime.substring(0, 10);
        } else if (Objects.equals(type, "month")) {
            sDate = currentTime.substring(0, 7);
        } else if (Objects.equals(type, "year")) {
            sDate = currentTime.substring(0, 4);
        }
        Map<String, Integer> result = homeService.getMemberChangeCount(type, sDate);
        return ResultInfo.success(result);
    }

    @ApiOperation(value = "7/14/30天", notes = "days=7、14、30,type=day、month、year")
    @PostMapping("/getAllCount")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "days", value = "7、14、30", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "day、month、year", dataType = "string", paramType = "query"),
    })
    public ResultInfo getAllCount(@RequestBody Map<String, Object> map) {
        int days = Integer.valueOf(map.get("days").toString());
        String type = map.get("type").toString();
        // 2020-07-08 15:12:30
        int notCardCount = homeService.getNotCardCount();
        int futureClassesCount = homeService.getFutureClassesCount(days);
        int memberExpirationCount = homeService.getMemberExpirationCount(days);
        int memberBirthdayCount = homeService.getMemberBirthdayCount(days);
        String currentTime = TimeUtils.getCurrentTime();
        String sDate = currentTime;
        if (Objects.equals(type, "day")) {
            sDate = currentTime.substring(0, 10);
        } else if (Objects.equals(type, "month")) {
            sDate = currentTime.substring(0, 7);
        } else if (Objects.equals(type, "year")) {
            sDate = currentTime.substring(0, 4);
        }
        Map<String, Integer> result = homeService.getMemberChangeCount(type, sDate);
        result.put("notCardCount", notCardCount);
        result.put("futureClassesCount", futureClassesCount);
        result.put("memberExpirationCount", memberExpirationCount);
        result.put("memberBirthdayCount", memberBirthdayCount);
        return ResultInfo.success(result);
    }

    @ApiOperation(value = "本周/月/年新增会员数、失效会员数", notes = "本周/月/年新增会员数、失效会员数")
    @PostMapping("/getMemberTrend")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "week、month、year", dataType = "string", paramType = "query"),
    })
    public ResultInfo getMemberTrend(@RequestBody Map<String, Object> map) {
        String type = map.get("type").toString();
        // 2020-07-08 15:12:30
        String currentTime = TimeUtils.getCurrentTime();
        String sDate = currentTime;
        if (Objects.equals(type, "week")) {
            sDate = currentTime.substring(0, 10);
        } else if (Objects.equals(type, "month")) {
            sDate = currentTime.substring(0, 7);
        } else if (Objects.equals(type, "year")) {
            sDate = currentTime.substring(0, 4);
        }
        List<Map<String, Object>> list = new ArrayList<>();
        List<MemberChangeBo> addTrendList = homeMapper.getMemberAddTrend(type, sDate);
        List<MemberChangeBo> invalidTrendList = homeMapper.getMemberInvalidTrend(type, sDate);

        Integer[] addDatas = null;
        Integer[] invalidDatas = null;
        if (Objects.equals(type, "week")) {
            addDatas = new Integer[7];
            invalidDatas = new Integer[7];
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Calendar calendar = Calendar.getInstance();
            while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
                calendar.add(Calendar.DAY_OF_WEEK, -1);
            }
            String[] dates = new String[7];
            for (int i = 0; i < 7; i++) {
                dates[i] = dateFormat.format(calendar.getTime());
                calendar.add(Calendar.DATE, 1);
            }
            for(int j=0;j<dates.length;j++){
                addDatas[j] = 0;
                invalidDatas[j] = 0;
            }
            for(int j=0;j<dates.length;j++){
                for(MemberChangeBo bo : addTrendList){
                    if(Objects.equals(bo.getDays(),dates[j])){
                        addDatas[j] = Integer.parseInt(bo.getNum());
                        break;
                    }
                }
            }
            for(int j=0;j<dates.length;j++){
                for(MemberChangeBo bo : invalidTrendList){
                    if(Objects.equals(bo.getDays(),dates[j])){
                        invalidDatas[j] = Integer.parseInt(bo.getNum());
                        break;
                    }
                }
            }

        } else if (Objects.equals(type, "month")) {
            sDate = currentTime.substring(0, 7);// 2020-07
            int endDay = getMonthDays(sDate);
            addDatas = new Integer[endDay];
            invalidDatas = new Integer[endDay];
            for (int j = 0; j < endDay; j++) {
                addDatas[j] = 0;
                invalidDatas[j] = 0;
            }
            for (int j = 1; j <= endDay; j++) {
                String day =  "";
                if (j < 10) {
                    day = "0" + j;
                } else {
                    day = "" + j;
                }
                String  key = sDate+"-"+day;
                for(MemberChangeBo bo : addTrendList){
                    if(Objects.equals(bo.getDays(),key)){
                        addDatas[j-1] = Integer.parseInt(bo.getNum());
                        break;
                    }
                }
                for(MemberChangeBo bo : invalidTrendList){
                    if(Objects.equals(bo.getDays(),key)){
                        invalidDatas[j-1] = Integer.parseInt(bo.getNum());
                        break;
                    }
                }
            }

        } else if (Objects.equals(type, "year")) {
            sDate = currentTime.substring(0, 4);
            String[]  months = {sDate+"-01",sDate+"-02",sDate+"-03",sDate+"-04",sDate+"-05",sDate+"-06",sDate+"-07",
                    sDate+"-08",sDate+"-09",sDate+"-10",sDate+"-11",sDate+"-12"};
            addDatas = new Integer[12];
            invalidDatas = new Integer[12];
            for (int j = 0; j < months.length; j++) {
                addDatas[j] = 0;
                invalidDatas[j] = 0;
                String key = months[j];//2020-01
                for(MemberChangeBo bo : addTrendList){
                    if(Objects.equals(bo.getDays(),key)){
                        addDatas[j] = Integer.parseInt(bo.getNum());
                        break;
                    }
                }
                for(MemberChangeBo bo : invalidTrendList){
                    if(Objects.equals(bo.getDays(),key)){
                        invalidDatas[j] = Integer.parseInt(bo.getNum());
                        break;
                    }
                }
            }
        }
        Map<String, Object> addMap = new HashMap<>();
        addMap.put("name", "新增会员数");
        addMap.put("data", addDatas);
        list.add(addMap);
        Map<String, Object> invalidMap = new HashMap<>();
        invalidMap.put("name", "失效员数");
        invalidMap.put("data", invalidDatas);
        list.add(invalidMap);
        return ResultInfo.success(list);
    }



    private int getMonthDays(String countDate) {
        int year = Integer.parseInt(countDate.substring(0, countDate.indexOf('-')));
        int mth = Integer.parseInt(countDate.substring(countDate.indexOf('-')+1));
        Calendar calendar = Calendar.getInstance();
        calendar.clear();//在使用set方法之前，必须先clear一下，否则很多信息会继承自系统当前时间
        calendar.set(Calendar.YEAR,year);
        calendar.set(Calendar.MONTH,mth-1);//Calendar对象默认一月为0          
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);//获得本月的天数
    }

}
